package com.primb.netlibs.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.primb.netlibs.entity.HttpResultResponse;
import com.primb.netlibs.exception.ExceptionEngine;
import com.primb.netlibs.exception.ResultException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.ParseException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.tencent.bugly.crashreport.crash.c.e;

final class FastJsonResponseBodyConvert<T> implements Converter<ResponseBody, T> {

    private Type type;

    public FastJsonResponseBodyConvert(Type type) {
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
//        boolean contains = response.contains("<!DOCTYPE");
//        if (contains) {
//            throw new ResultException(ExceptionEngine.LOGIN_TIMEOUT, "登录已失效，请重新登录");
//        }
//        boolean isDelete = response.contains("删除成功");
//        boolean isApproval = response.contains("审批成功");
//        boolean isChar = response.contains("{");
//        if (isDelete && !isChar && type == String.class) {
//            return (T) response;
//        }
//        if (isApproval && !isChar && type == String.class) {
//            return (T) response;
//        }
//        if (!isDelete && !isChar && type == String.class) {
//            throw new ResultException(response);
//        }
//        if (!isApproval && !isChar && type == String.class) {
//            throw new ResultException(response);
//        }
//
//        boolean isSave = response.contains("申请信息已保存");
//        if (isSave && type == String.class) {
//            return (T) response.replace("{", "").replace("}", "");
//        }
//        if (!isSave && type == String.class) {
//            throw new ResultException(response.replace("{", "").replace("}", ""));
//        }
        T t = null;
        try {
//            //ResultResponse 只解析result字段
//            HttpResultResponse resultResponse = JSON.parseObject(response, HttpResultResponse.class);
//            if (resultResponse.getSuccess().equals("true")) {
            //result==true表示成功返回，继续用本来的Model类解析
            try {
                HttpResultResponse httpResultResponse = JSON.parseObject(response, HttpResultResponse.class);
                if (httpResultResponse.isSuccess())
                    t = JSON.parseObject(response, type);
                else if (httpResultResponse.getMsgCode() != null && "0000".equals(httpResultResponse.getMsgCode())) {
                    throw new ResultException(ExceptionEngine.LOGIN_TIMEOUT, "登录已失效，请重新登录");
                } else {
                    throw new ResultException(httpResultResponse.getMsg());
                }
//                if (type instanceof Object) {
//                    try {
//                        HttpResultResponse httpResultResponse = JSON.parseObject(response, HttpResultResponse.class);
//                        if (!httpResultResponse.isSuccess()) {
//                            //ErrResponse 将msg解析为异常消息文本
//                            ResultException resultException = new ResultException(httpResultResponse.getMsg());
//                            if (httpResultResponse.getMsgCode() != null && "0000".equals(httpResultResponse.getMsgCode())) {
//                                resultException.code = ExceptionEngine.LOGIN_TIMEOUT;
//                            }
//                            throw resultException;
//                        }
//                    } catch (Exception e) {
//                    }
//                }
            } catch (Exception e) {
//                if (e instanceof JSONException || e instanceof ParseException) {
//                    HttpResultResponse httpResultResponse = JSON.parseObject(response, HttpResultResponse.class);
//                    if (!httpResultResponse.isSuccess()) {
//                        //ErrResponse 将msg解析为异常消息文本
//                        ResultException resultException = new ResultException(httpResultResponse.getMsg());
//                        if (httpResultResponse.getMsgCode() != null && "0000".equals(httpResultResponse.getMsgCode())) {
//                            resultException.code = ExceptionEngine.LOGIN_TIMEOUT;
//                        }
//                        throw resultException;
//                    } else {
                throw e;
//                    }
//                } else {
//                    throw new ResultException(e.getMessage());
//                }
            }
            return t;
        } finally {

        }
    }
}
